package com.lintpanzer.managers
{
	import com.lintpanzer.events.NativeMenuEvent;
	
	import flash.desktop.NativeApplication;
	import flash.display.NativeMenu;
	import flash.display.NativeMenuItem;
	import flash.display.NativeWindow;
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.ui.Keyboard;
	
	public class NativeMenuManager extends ManagerBase
	{	
		public function NativeMenuManager(target:IEventDispatcher=null)
		{
			super(target);
		}
		
		public function buildApplicationMenus(targetWindow:NativeWindow):void{
			var rootMenu:NativeMenu;
			
			if (NativeApplication.supportsMenu){
				rootMenu = NativeApplication.nativeApplication.menu;
			} else if (NativeWindow.supportsMenu){
				targetWindow.menu = rootMenu = new NativeMenu();
			}
			
			if (rootMenu){
				rootMenu.addSubmenu(fileMenu(),"File");
				//rootMenu.addSubmenu(editMenu(),"Edit");
				rootMenu.addSubmenu(helpMenu(),"Help");
			}
		}
		
		private function addKeyBinding(nativeMenuItem:NativeMenuItem,keyEquivalent:String):void{
			nativeMenuItem.keyEquivalent = keyEquivalent;
			if (NativeApplication.supportsMenu){
				nativeMenuItem.keyEquivalentModifiers = [Keyboard.COMMAND];
			} else if (NativeWindow.supportsMenu){
				nativeMenuItem.keyEquivalentModifiers = [Keyboard.CONTROL];
			}
		}
		
		private function fileMenu():NativeMenu{
			var fileMenu:NativeMenu = new NativeMenu();
			var openMenu:NativeMenuItem = fileMenu.addItem(new NativeMenuItem("Open"));
				addKeyBinding(openMenu,"o");
				openMenu.addEventListener(Event.SELECT,function(event:Event):void{
					dispatchEvent(new NativeMenuEvent(NativeMenuEvent.OPEN_SELECTED));
				});
			var exitMenu:NativeMenuItem = fileMenu.addItem(new NativeMenuItem("Exit"));
				exitMenu.addEventListener(Event.SELECT,function(event:Event):void{
					dispatchEvent(new NativeMenuEvent(NativeMenuEvent.EXIT_SELECTED));
				});
			return fileMenu;
		}
		
		private function editMenu():NativeMenu{
			var editMenu:NativeMenu = new NativeMenu();
			var cutMenu:NativeMenuItem = editMenu.addItem(new NativeMenuItem("Cut"));
				addKeyBinding(cutMenu,"x");
				cutMenu.addEventListener(Event.SELECT,function(event:Event):void{
					dispatchEvent(new NativeMenuEvent(NativeMenuEvent.CUT_SELECTED));
				});
			var copyMenu:NativeMenuItem = editMenu.addItem(new NativeMenuItem("Copy"));
				addKeyBinding(copyMenu,"c");
				copyMenu.addEventListener(Event.SELECT,function(event:Event):void{
					dispatchEvent(new NativeMenuEvent(NativeMenuEvent.COPY_SELECTED));
				});
			var pasteMenu:NativeMenuItem = editMenu.addItem(new NativeMenuItem("Paste"));
				addKeyBinding(pasteMenu,"v");
				pasteMenu.addEventListener(Event.SELECT,function(event:Event):void{
					dispatchEvent(new NativeMenuEvent(NativeMenuEvent.PASTE_SELECTED));
				});
			return editMenu;
		}
		
		private function helpMenu():NativeMenu{
			var helpMenu:NativeMenu = new NativeMenu();
			var aboutMenu:NativeMenuItem = helpMenu.addItem(new NativeMenuItem("About LintPanzer"));
				aboutMenu.addEventListener(Event.SELECT,function(event:Event):void{
					dispatchEvent(new NativeMenuEvent(NativeMenuEvent.ABOUT_SELECTED));
				});
			return helpMenu;
		}
	}
}